 import messageUtil from "./messageUtil"
 import data from "../api/data" 

 const apiHost = 'https://www.sx2b2c.com' // 接口地址

function post (url, params, callBack, errorCallBack){
	let requestUrl = apiHost + url;
 
	my.request({
		url: requestUrl,
		data: params,
    dataType:"json",
		method: 'POST',
		header: {
			'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
			'token': getToken()
		},
		success: ({
			data
		}) => {
			// console.log('post1', data)
			if (data.code == 0) {
				console.log('post2', data)
        messageUtil.closeLoading()
        messageUtil.closeMsg()
        messageUtil.showMsg(data.msg)
 
				if (errorCallBack) {
					errorCallBack(data)
				}
				return;
			} else if (data.code == -1) {
        messageUtil.closeLoading()
        messageUtil.closeMsg()
        messageUtil.showFail("登录失效，请重新登录")
			 
				clearToken()
				setTimeout(() => {
          my.navigateTo({
            url: '/pages/index/index'
          })
					
				}, 2000)
				return;
			}
      if(callBack){
        callBack(data);
      }
			
		},
		fail: (err) => {
			// console.log(url + ' post fail', err);
			let data = err.data
			if (data.code == 401) {
        my.clearStorageSync()
				
			}
			if (errorCallBack) {
				errorCallBack(err)
			}

		}
	})
}

function postReturnSource (url, params, callBack, errorCallBack){
	let requestUrl = apiHost + url;
	 
	my.request({
		url: requestUrl,
		data: params,
		method: 'POST',
		header: {
			'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
			'token': getToken()
		},
		success: ({
			data
		}) => {
			if (callBack) {
				callBack(data);
			}
		},
		fail: (err) => {
			// console.log(url + ' postReturnSource fail', err);
			let data = err.data
			if (data.code == 401) {
				
        my.clearStorageSync
			}
		}
	})
}

function get(url, params, callBack, errorCallBack) {
	let requestUrl = apiHost + url;
 
	my.request({
		url: requestUrl,
		data: params,
		method: 'GET',
		header: {
			'token': getToken()
		},
		success: ({
			data
		}) => {
			if (data.code == 0) {
        messageUtil.closeLoading()
        messageUtil.closeMsg()
        messageUtil.showMsg(data.msg)
			 
				if (errorCallBack) {
					errorCallBack(data)
				}
				return;
			} else if (data.code == -1) {
        messageUtil.closeLoading()
        messageUtil.closeMsg()
        messageUtil.showFail('登录失效，请重新登录')

				 
				clearToken()
				setTimeout(() => {
					my.navigateTo({
						url: '/pages/index/index'
					})
				}, 2000)
				return;
			}
      if(callBack){
        callBack(data);
      }
			
		},
		fail: (err) => {
			// console.log(url + 'get fail', err);
			let data = err.data
			if (data.code == 401) {
        my.clearStorageSync()
			}
		}
	})
}

function getToken() {
	let token = my.getStorageSync('token')
	return token;
}
function setToken (token) {
  
	my.setStorageSync('token', token)
}
function clearToken  (){
	my.removeStorageSync('token')
}

function getA  ()  {
	let a = my.getStorageSync('a')
	
	return a;
}
function setA  (a) {
	my.setStorageSync('a', a)
}

function isSetA  ()  {
	let a = my.getStorageSync('a')
	if (!a) {
		return a;
	}
	return true;
}

module.exports = {
	post,
	postReturnSource,
	get,
	getToken,
	setToken,
	clearToken,
	isSetA,
	getA,
	setA,
}